#include "movable.h"

MovableList::MovableList()
{
  head = NULL;
  back = NULL;
  size = 0;
}

void MovableList::insertFront(Movable* d)
{
  size = size + 1;
  MLNode* temp = new MLNode();
  temp->data = d;
  temp->last = NULL;
  if (head == NULL)
  {
    temp->next = NULL;
    head = temp;
    back = temp;
  }
  else
  {
    temp->next = head;
    head->last = temp;
    head = temp;
  }
//  sort();
}

void MovableList::insertBack(Movable* d)
{
  size = size + 1;
  MLNode* temp = new MLNode();
  temp->data = d;
  temp->next = NULL;
  if (back == NULL)
  {
    temp->last = NULL;
    head = temp;
    back = temp;
  }
  else
  {
    temp->last = back;
    back->next = temp;
    back = temp;
  }
//  sort();
}

Movable* MovableList::popFront()
{
  if (head == NULL)
    return NULL;
  MLNode* temp = head;
  head = head->next;
  size = size - 1;
  if (head == NULL)
  {
    back = NULL;
    return temp->data;
  }
  head->last = NULL;
  return temp->data;
}

Movable* MovableList::popBack()
{
  if (back == NULL)
    return NULL;
  MLNode* temp = back;
  back = back->last;
  size = size - 1;
  if (back == NULL)
  {
    head == NULL;
    return temp->data;
  }
  back->next = NULL;
  return temp->data;
}

bool MovableList::empty()
{
  return size == 0;
}

/*
void MovableList::swap(MLNode* a, MLNode* b)
{
  a->next = b->next;
  b->last = a->last;
  a->last = b;
  b->next = a;
  if (a == head)
  {
    head = b;
    if (b == back)
      back = a;
    return;
  }
  if (b == back)
  {
    back = a;
    return;
  }
  return;
}

void MovableList::sort()
{
  MLNode* thisHead = head;
  while (thisHead->next->next != NULL)
  {
    if (!thisHead->data->isPlayer() && thisHead->next->data->isPlayer())
    {
       swap(thisHead, thisHead->next);
    }

    thisHead = thisHead->next;
  }
}*/
